package com.quick.develop.flowable.framework.core;

import com.quick.develop.flowable.framework.cmd.ComplementHistoricTaskCmd;
import com.quick.develop.flowable.framework.factory.FlowServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * desc:
 *
 * @author junyuan.liu
 * @date 2023/12/28 12:52
 */
@Component
@Slf4j
public class BpmEntityDeletedListener extends FlowServiceFactory implements FlowableEventListener {
    @Value("${flowable.custom.comp-histask.open:#{true}}")
    private boolean open = false;

    @Override
    public void onEvent(FlowableEvent flowableEvent) {
        Object entity = ((FlowableEntityEventImpl) flowableEvent).getEntity();
        if ((entity instanceof TaskEntityImpl) && open) {
            managementService.executeCommand(new ComplementHistoricTaskCmd(((TaskEntityImpl) entity).getId()));
        }

    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}
